package gui;

import java.awt.Color;
import javax.swing.BorderFactory;
import ptolemy.plot.Plot;
import register.Register;

/**
 * Panel used to display the state of the registers.
 * @author Matt Cole, Alex Bush
 */
public class VectorPlot extends Plot {
	/** Angle to rotate the searched state by each iteration */
	private double theta;
	/** Current angle of the searched state */
	private double stateangle;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Construct the vectorplot object
	 * @param width width of the vector plot
	 */
	public VectorPlot(int width) {
		
		// Format the plot area
		setSize(width, 500);
		setBackground(Color.white);
		setBorder(BorderFactory.createTitledBorder("Vector Representation"));
		
	}
	
	/**
	 * Sets up the vector plot for the given register and search element.
	 * @param reg
	 * @param searchElem
	 */
	public void setup(Register reg, int searchElem) {
		
		setYRange(-1,1);
		setXRange(-1,1);
		
		// Plot colours
		Color c[] = new Color[3];
		c[0] = Color.green;
		c[1] = Color.red;
		c[2] = Color.black;
		setColors(c);
		
		// Searched state data point
		addPoint(0, 0, 0, false);
		addPoint(0, 0, 1, true);
		addLegend(0,"|"+searchElem+">");
		addLegend(0,"Searched");
		
		// Arbitrary state data point
		addPoint(1, 0, 0, false);
		addPoint(1, 1, 0, true);
		addLegend(1,"|Φ>");
		addLegend(1,"Orthogonal");
		
		// Register state
		addLegend(2,"|μ>");
		addLegend(2,"Register");
		// Find angle state is rotated by each iteration.
		theta = 2*Math.asin(1.0/Math.sqrt(reg.getNumStates()));
		stateangle = -theta/2;
		
	}

	/**
	 * Updates the state vector (increases the state angle by a set amount).
	 */
	public void update() {
		// Clear datapoints for register state and plot
		clear(2);
		stateangle += theta;	
		addPoint(2,0,0,false);
		addPoint(2,Math.cos(stateangle),Math.sin(stateangle),true);
	}
	
	/**
	 * Removes only the register stare vector from the vector plot.
	 */
	public void removeRegState() {
		clear(2);
	}

	/**
	 * Clears all data from plot.
	 */
	public void reset() {
		clear(2);
		clear(true);
		stateangle = -theta/2;
	}
}
